<?php

/**
 * test case for classe databean
 * @author J.Ducastel <jeremie@ducastel.name>
 * @todo tests for a child class, with input filters etc
 */
class TestDataBean extends UnitTestCase {
	
	function setUp() {
		$this->data=array(
			'var'=>'value'
			,'case'=>'CaSe'
			,'number'=>42);
		$this->newData=array(
			'var'=>'newValue'
			, 'case'=>'cAsE'
			,'number'=>43
			,'newVar'=>'a new value');
	}
	
	function testConstructor() {
		$bean=$this->makeBean();
		$this->assertIsA($bean,'databean');
		foreach ($this->data as $key => $value) {
			$this->assertEqual($bean->$key,$value);
		}
	}
	
	function testGet() {
		$bean=$this->makeBean();
		$this->assertEqual($bean->get('var'),'value');
		$this->assertEqual($bean->get('case'),'CaSe');
		$this->assertEqual($bean->get('number'),42);
		// alias get / getMember
		$this->assertEqual($bean->getMember('var'),$bean->get('var'));
		if ($this->isPHP5()) {
			$this->assertEqual($bean->var,'value');
			$this->assertEqual($bean->case,'CaSe');
			$this->assertEqual($bean->number,42);
		}
	}
	
	function testGetAll() {
		$bean=$this->makeBean();
		$this->assertEqual($this->data,$bean->getAllAssoc());
		$this->assertEqual($this->data,$bean->getAll());
	}
	
	function testSet() {
		$bean=$this->makeBean();
		$this->assertTrue($bean->set('var','newValue'));
		$this->assertEqual($bean->get('var'),'newValue');
		// and setting a new member
		$this->assertTrue($bean->set('newVar','value'));
		// now setting as null
	}
	
	function testMembersLock() {
		$bean=$this->makeBean();
		// testing defaults
		$this->assertFalse($bean->membersLock(),"members should not be locked by default");
		$this->assertFalse($bean->membersLock('weird'),"could lock members with a non boolean");
		$this->assertTrue($bean->memberSettable('newVar'));
		// locking members
		$this->assertTrue($bean->membersLock(true),"could not lock members");
		$this->assertFalse($bean->memberSettable('newVar'));
		// testing setting new members
		$this->assertFalse($bean->set('newVar','value'));
		// but defined key should still be settable
		$this->assertTrue($bean->memberSettable('var'));
		$this->assertTrue($bean->set('var','newValue'),'could not set a defined key');
		// and you could still load members in array
		$this->assertTrue($bean->loadMembers($this->newData),'could not load values array when members locked');
	}
	
	function testValuesLock() {
		$bean=$this->makeBean();
		// testing defaults
		$this->assertFalse($bean->valuesLock(),"values should not be locked by default");
		$this->assertFalse($bean->valuesLock('weird'),"could lock values with a non boolean");
		// locking values
		$this->assertTrue($bean->valuesLock(true),"could not lock values");
		// testing setting new values
		$this->assertFalse($bean->set('var','newValue'),"could set a new value");
	}
	
	function testLoad() {
		$bean=$this->makeBean();
		$bean2=$this->makeBean();
		$this->assertTrue($bean->loadMembers($this->newData));
		$this->assertEqual($bean->get('var'),'newValue');
		$this->assertEqual($bean->get('number'),43);
		// loadmembers alias
		$this->assertTrue($bean2->loadData($this->newData));
		$this->assertEqual($bean,$bean2);
	}
	
	function testDefaults() {
		$bean=$this->makeBean();
		// setting default for a new member
		$this->assertTrue($bean->setMemberDefault('newVar','a new value'));
		$this->assertEqual($bean->get('newVar'),'a new value');
		// setting default for an old member
		$this->assertFalse($bean->setMemberDefault('var','default'));
		$this->assertNotEqual($bean->get('var'),'default');
		// loading array
		$this->assertIsA($bean->loadDefaults($this->newData),'int');
		$this->assertEqual($bean->get('newVar'),'a new value');
		$this->assertNotEqual($bean->get('var'),'newValue');
	}
	
	function testListMembers() {
		$bean=$this->makeBean();
		$this->assertEqual($bean->listMembers(),array_keys($this->data));
	}
	
	function testGettable() {
		$b=$this->makeBean();
		$this->assertTrue($b->memberGettable('var'));
		$this->assertTrue($b->memberGettable('case'));
		$this->assertTrue($b->memberGettable('number'));
		
		$this->assertFalse($b->memberGettable('newVar'),'non-existing members should not be gettable');
	}
	
	function testSettable() {
		$b=$this->makeBean();
		$this->assertTrue($b->memberSettable('var'));
		$this->assertTrue($b->memberSettable('case'));
		$this->assertTrue($b->memberSettable('number'));
		
		$this->assertTrue($b->memberSettable('newVar'),'new members should be settable when not locked');
		$b->membersLock(true);
		$this->assertFalse($b->memberSettable('newVar'),'new members should not be settable when locked');
		
		$b->valuesLock(true);
		$this->assertFalse($b->memberSettable('var'));
		$this->assertFalse($b->memberSettable('case'));
		$this->assertFalse($b->memberSettable('number'));
		// $this->dump($b);
		// $b->valuesLock(false);
	}
	
	
	function & makeBean() {
		$bean=new DataBean($this->data);
		return $bean;
	}
	
	function isPHP5() {
		return substr(phpversion(),0,1)=='5';
	}
}

?>